package eteam.aps.systemservice.service;

import cn.hutool.core.convert.Convert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import eteam.aps.common.QueryResult;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.systeminterface.model.dto.RoleDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.ModuleoperEntity;
import eteam.aps.systeminterface.model.entity.UserEntity;
import eteam.aps.systeminterface.model.enums.StateEnum;
import eteam.aps.systeminterface.model.reqparam.*;
import eteam.aps.systeminterface.service.IRoleController;
import eteam.aps.systemservice.bll.RoleBll;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 角色服务
 */
@Api(tags = "系统服务控制器")
@RestController
//@RequestMapping(value = "/api/role")
public class RoleController implements IRoleController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RoleBll roleBll;

    /**
     * 分页查询角色列表
     *
     * @param requestDto: 查询参数
     * @return
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页查询角色列表")
    @Override
    @GetMapping(value = "/api/role/querypagelist")
    public R<QueryResult<UserEntity>> queryRolePageList(@Validated QueryRoleParam requestDto) throws Exception, AuthException {
        return R.success(roleBll.queryRolePageList(requestDto));
    }

    /**
     * 新增角色
     *
     * @param roleDto: 角色信息
     * @return
     */
    @ApiOperationSupport(order = 2, ignoreParameters = {"roleDto.fuserid"})
    @ApiOperation(value = "新增角色")
    @Override
    @PostMapping(value = "/api/role/createrole")
    public R createRole(@Validated(Insert.class) @RequestBody RoleDto roleDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        roleBll.createRole(tokenDto, roleDto);
        return R.success();
    }

    /**
     * 更新角色
     *
     * @param roleDto: 角色信息
     * @return
     */
    @ApiOperationSupport(order = 3, ignoreParameters = {})
    @ApiOperation(value = "更新角色")
    @Override
    @PostMapping(value = "/api/role/updaterole")
    public R updateRole(@Validated(Update.class) @RequestBody RoleDto roleDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        roleBll.updateRole(tokenDto, roleDto);
        return R.success();
    }

    /**
     * 删除角色
     *
     * @param roleParam: 角色Id参数
     * @return
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除角色")
    @Override
    @PostMapping(value = "/api/role/deleterole")
    public R deleteRole(@Validated @RequestBody IdParam roleParam) throws Exception, AuthException {
        roleBll.deleteRole(roleParam.getObjid());
        return R.success();
    }

    /**
     * 变更状态，启用/禁用
     *
     * @param changeParam: 变更状态参数
     * @return
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "变更角色状态")
    @Override
    @PostMapping(value = "/api/role/changerolestate")
    public R changeRoleState(@Validated @RequestBody ChangeStateParam changeParam) throws Exception, AuthException {
        roleBll.changeState(changeParam.getObjid(), StateEnum.values()[changeParam.getState()]);
        return R.success();
    }

    /**
     * 根据角色ID获取所属用例列表
     *
     * @param roleid: 角色ID
     * @return
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "变更角色状态")
    @Override
    @GetMapping(value = "/api/role/getucbyroleid")
    public R<List<ModuleoperEntity>> getUcByRoleId(@NotBlank(message = "角色ID为空") String roleid) throws Exception, AuthException {
        return R.success(roleBll.getUcByRoleId(roleid));
    }

    /**
     * 保存角色用例
     *
     * @param roleUcParam: 角色用例请求参数
     * @return
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "设置角色用例")
    @Override
    @PostMapping(value = "/api/role/setroleuc")
    public R setRoleUc(@Validated @RequestBody RoleUcParam roleUcParam) throws Exception, AuthException {
        roleBll.setRoleUc(roleUcParam.getRoleid(), roleUcParam.getRoleuc());
        return R.success();
    }

    /**
     * 根据机构ID和机构类别，获取所属角色列表
     *
     * @param userRoleParam
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "根据机构ID和机构类别，获取所属角色列表")
    @Override
    @GetMapping(value = "/api/role/getrolelistbytype")
    public R<List<UserEntity>> getRoleListByType(@Validated UserRoleParam userRoleParam) throws Exception, AuthException {
        return R.success(roleBll.getRoleListByType(userRoleParam));
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "获取角色列表")
    @Override
    @GetMapping(value = "/api/role/getrolelist")
    public R<List<UserEntity>> getRoleList() throws Exception, AuthException {
        return R.success(roleBll.getRoleList());
    }

    /**
     * 根据用户ID，获取绑定角色列表
     *
     * @param userid: 用户ID
     * @return
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "根据用户ID，获取绑定角色列表")
    @Override
    @GetMapping(value = "/api/role/getrolelistbyuserid")
    public R<List<UserEntity>> getRoleListByUserId(@NotBlank(message = "用户ID为空") String userid) throws Exception, AuthException {
        return R.success(roleBll.getRoleListByUserId(userid));
    }

    /**
     * 保存角色用户
     *
     * @param roleUserParam: 角色用户请求参数
     * @return
     */
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "设置角色用例")
    @Override
    @PostMapping(value = "/api/role/setroleuser")
    public R setRoleUser(@Validated @RequestBody RoleUserParam roleUserParam) throws Exception, AuthException {
        roleBll.setRoleUser(roleUserParam.getUserid(), roleUserParam.getRoleids());
        return R.success();
    }
}
